<script src="{{ getAssets }}js/amazeui.datatables.min.js?v={{ assets_version }}"></script>
<script>
    var allocation_id = '{{.allocation.Id}}';
    var tables = $('#dataTables').DataTable({
        "sAjaxSource" : '{{ urlfor "ProductsController.DataGridForExtDep"}}'
        ,"bLengthChange": false
        ,"fnServerData" : function(sSource, aoData, fnCallback) {
            var params = checkQueryTableData(aoData);
            $('#search_key').val() ? params.filter = $('#search_key').val() : '';
            params.TypeId = defErpTypeid;
            params.Status = 1;
            $.post(sSource, JSON.stringify(params),function(resp) {
                fnCallback(resp);
            },'json')
        }
        ,columns: [
            {"data" : "Id", "bSortable": false}
            ,{render:function(data, type, full, meta) {
                    return productGroupName(full);
                }}
            ,{
                "mDataProp": "Action",
                "bSortable": false,
                "sWidth":"10%",
                "render": function ( data, type, full, meta ) {
                    btns = '<a class="am-btn am-btn-secondary am-btn-xs open-dialog-btn" data-entryid="{{.allocation.Id}}" data-goodsid="'+ full.Id +'"><i class="am-icon-shopping-cart"></i></a>';
                    return btns;
                }
            }
        ]
        ,"aLengthMenu":[20, 50, 100, 200]
        ,'bLengthChange': true
        ,"serverSide": true
        ,"bPaginate" : true
        ,"bFilter" : false	//是否启动过滤、搜索功能
        ,"bInfo" : true //是否显示页脚信息，DataTables插件左下角显示记录数
        ,"bProcessing" : true //DataTables载入数据时，是否显示‘进度’提示
        ,"drawCallback": function(settings){
            $('.open-dialog-btn').click(function(){
                var goodsid = $(this).data('goodsid');
                var url = '{{urlfor "AllocationController.SelectEntry"}}?allocation_id='+allocation_id+'&products_id='+goodsid;
                var index = layer.open({
                    type: 2,
                    title: '选择库存',
                    shadeClose: false,
                    shade: 0.2,
                    maxmin: true,
                    shift: 1,
                    area: ['100%', '100%'],
                    content: url
                });
                layer.full(index);
            });
        }
    });

    $("#searchsubmit").bind("click", function () { //点击按钮 触发table重新请求服务器
        $("#dataTables").dataTable().fnDraw(false);
    });

    //类别切换事件
    function defErpTypeidEvent(obj) {
        defErpTypeid= $(obj).data('id');
        $("#searchsubmit").click();
    }

    $('#cennelBtn').click(function(){
        history.go(-1);
    });

    function closeSelectComplete() {
        layer.closeAll();
        $.post('{{ urlfor "AllocationController.GetMiddleList" }}', {'allocation_id':allocation_id}, function(res){
            if(res.code == 0) {
                //goods-list
                $('#goods-list').html('');
                $.each(res.data, function(index, value){
                    var html = '<div class="list">\
                        <p class="am-text-sm">'+ productGroupName(value.Products) +'</p>\
                        <p>X'+ value.PurchaseNum +' <a href="#" data-entry="'+ value.EntryId +'" data-goods="'+ value.Products.Id +'" data-attr="'+ encodeURIComponent(value.Attribute) +'" data-id="'+ value.Id +'" data-typeid="'+ value.Products.TypeId +'" onclick="editMiddleData(this);return false;" class="am-text-xs">编辑</a>&nbsp;&nbsp;&nbsp;<a href="#" onclick="deleteMiddleData('+ value.Id +', this);return false;" class="am-text-danger am-text-xs">删除</a></p>\
                    </div>';

                    $('#goods-list').append(html);
                });
            }
        }, 'json');
    }

    function editMiddleData(h) {
        var o = $(h);
        var entry_id = o.data('entry');
        var goods_id = o.data('goods');
        var middle_id = o.data('id');
        var url = '{{urlfor "AllocationController.SelectEntry"}}?allocation_id='+allocation_id+'&products_id='+goods_id+'&middle_id='+middle_id;
        var index = layer.open({
            type: 2,
            title: '编辑库存',
            shadeClose: false,
            shade: 0.2,
            maxmin: true,
            shift: 1,
            area: ['100%', '100%'],
            content: url
        });
    }

    function deleteMiddleData(middle_id, h) {
        $.post('{{ urlfor "AllocationController.DeleteMiddleData" }}', {'middle_id':middle_id}, function(res){
            if(res.code == 0) {
                $(h).parent().parent().remove();return;
            } else {
                layer.msg(res.msg);
            }
        }, 'json');
    }
    //提交订单
    $('#submitOrder').click(function(){
        $.post('{{ urlfor "AllocationController.SubmitOrder" }}', {'allocation_id':allocation_id}, function(res) {
            if(res.code == 1) {
                layer.alert(res.msg);
                return false;
            }
            location = '{{ urlfor "AllocationController.Index" }}';
        },'json');

    });

    closeSelectComplete();
</script>